package com.ncs.cns.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.ncs.cns.R;
import com.ncs.cns.business.db.DBHelper;
import com.ncs.cns.business.http.HttpHelper;
import com.ncs.cns.business.service.CommonService;
import com.ncs.cns.business.service.EventService;
import com.ncs.cns.business.service.ResourceService;
import com.ncs.cns.business.service.UserService;
import com.ncs.cns.business.service.UserSessionService;
import com.ncs.cns.business.util.StringUtils;
import com.ncs.cns.framework.CnsApplication;
import com.ncs.cns.framework.SysApplication;

public class BaseActivity extends FragmentActivity {

	protected CnsApplication application = null;
	
	protected HttpHelper httpHelper;
	protected CommonService commService;
	protected UserSessionService sessionService;
	protected UserService userService;
	protected ResourceService resourceService;
	protected EventService eventService;
	protected DBHelper dbHelper;
	protected long exitTime = 0;
	protected String currentUserId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SysApplication.getInstance().addActivity(this);
		application = (CnsApplication) getApplication();
		commService = new CommonService(this);
		sessionService = new UserSessionService(this.getApplicationContext());
		userService =new UserService(this.getApplicationContext());
		resourceService = new ResourceService(this.getApplicationContext());
		eventService = new EventService(this);
		httpHelper = new HttpHelper(this);
		dbHelper = new DBHelper(this.getApplicationContext());
		currentUserId = userService.getCurrentUser();
	}
	
	public boolean isNetworkAvailable(){
		return commService.isNetworkAvailable();
	}

	/**
	 * 描述：Toast提示文本.
	 * 
	 * @param resId
	 *            文本的资源ID
	 */
	public void showToast(int resId) {
		Toast.makeText(this, this.getResources().getText(resId),
				Toast.LENGTH_LONG).show();
	}
	
	public void showToastShort(int resId) {
		Toast.makeText(this, this.getResources().getText(resId),
				Toast.LENGTH_SHORT).show();
	}

	public void showToast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}
		
	protected void setUserIdTextChangeListener(final EditText editText,
			final ImageButton clear) {
		editText.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String str = editText.getText().toString().trim();
				int length = str.length();
				if (length > 0) {
					clear.setVisibility(View.VISIBLE);
					if (!StringUtils.isNumberLetter(str)) {
						str = str.substring(0, length - 1);
						editText.setText(str);
						String str1 = editText.getText().toString().trim();
						editText.setSelection(str1.length());
						showToast(R.string.error_user_id_expr);
					}
					clear.postDelayed(new Runnable() {

						@Override
						public void run() {
							clear.setVisibility(View.INVISIBLE);
						}

					}, 5000);

				} else {
					clear.setVisibility(View.INVISIBLE);
				}
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			public void afterTextChanged(Editable s) {

			}
		});
	}

	protected void setUserPwdTextChangeListener(final EditText editText,
			final ImageButton clear) {
		editText.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String str = editText.getText().toString().trim();
				int length = str.length();
				if (length > 0) {
					clear.setVisibility(View.VISIBLE);
					if (!StringUtils.isNumberLetter(str)) {
						str = str.substring(0, length - 1);
						editText.setText(str);
						String str1 = editText.getText().toString().trim();
						editText.setSelection(str1.length());
						showToast(R.string.error_pwd_expr);
					}
					clear.postDelayed(new Runnable() {
						@Override
						public void run() {
							clear.setVisibility(View.INVISIBLE);
						}

					}, 5000);
				} else {
					clear.setVisibility(View.INVISIBLE);
				}
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			public void afterTextChanged(Editable s) {

			}
		});
	}

	public CommonService getCommService() {
		return commService;
	}

	public void setCommService(CommonService commService) {
		this.commService = commService;
	}

	public UserSessionService getSessionService() {
		return sessionService;
	}

	public void setSessionService(UserSessionService sessionService) {
		this.sessionService = sessionService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public EventService getEventService() {
		return eventService;
	}

	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}
	
	/*
	 * @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the
	 * menu; this adds items to the action bar if it is present.
	 * getMenuInflater().inflate(R.menu.main, menu); return true; }
	 */
	
}
